package com.example.moneybook.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.moneybook.dto.ResponseResult;
import com.example.moneybook.dto.TransferDto;
import com.example.moneybook.enums.AppHttpCodeEnum;
import com.example.moneybook.mapper.AccountMapper;
import com.example.moneybook.mapper.TransferMapper;
import com.example.moneybook.pojo.Account;
import com.example.moneybook.pojo.Transfer;
import com.example.moneybook.service.AccountService;
import com.example.moneybook.service.TransferService;
import com.example.moneybook.vo.TransferVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 包
 * @Date: 2023/3/1 19:37
 * Description:
 */
@Service
public class TransferServiceImpl extends ServiceImpl<TransferMapper, Transfer> implements TransferService {

    @Autowired
    TransferMapper transferMapper;
    @Autowired
    AccountService accountService;
    @Override
    public ResponseResult addTransfer(TransferDto transferDto) {
        int fromId = accountService.getAccountId(transferDto.getUserId(), transferDto.getFromName());
        int toId = accountService.getAccountId(transferDto.getUserId(), transferDto.getToName());

        Account accountInfo = accountService.getAccountInfo(transferDto.getUserId(), toId);

        if (accountInfo.getAccountState() == 1 && accountInfo.getAccountMoney() - transferDto.getMoney() < 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"该借贷账户偿还金额过多");
        }

        Transfer transfer1 = new Transfer();
        transfer1.setUserId(transferDto.getUserId());
        transfer1.setFromId(fromId);
        transfer1.setToId(toId);
        transfer1.setTransferMoney(transferDto.getMoney());
        transfer1.setTransferState(1);
        transfer1.setRemarks(transferDto.getRemarks());
        transfer1.setTransferDate(transferDto.getTransferDate());
        transfer1.setTransferTime(transferDto.getTransferTime());

        Transfer transfer2 = new Transfer();
        transfer2.setUserId(transferDto.getUserId());
        transfer2.setFromId(toId);
        transfer2.setToId(fromId);
        transfer2.setTransferMoney(transferDto.getMoney());
        transfer2.setTransferState(0);
        transfer2.setRemarks(transferDto.getRemarks());
        transfer2.setTransferDate(transferDto.getTransferDate());
        transfer2.setTransferTime(transferDto.getTransferTime());

        int res1=transferMapper.insert(transfer1);
        int res2=transferMapper.insert(transfer2);

        accountService.updateAccountMoney(fromId, 1, transferDto.getMoney());
        accountService.updateAccountMoney(toId, 0, transferDto.getMoney());

        return ResponseResult.okResult(200, "添加成功");
    }

    @Override
    public List<TransferVo> transferByAccount(String userId, int accountId) {
        QueryWrapper<Transfer> transferQueryWrapper = new QueryWrapper<>();
        transferQueryWrapper
                .eq("user_id", userId)
                .eq("from_id", accountId);
        List<Transfer> transfers = transferMapper.selectList(transferQueryWrapper);
        List<TransferVo> transferVos = new ArrayList<>();
        for (Transfer transfer : transfers) {
            TransferVo transferVo = new TransferVo();
            BeanUtils.copyProperties(transfer, transferVo);
            String fromName = accountService.getAccountName(userId, transfer.getFromId());
            String toName = accountService.getAccountName(userId, transfer.getToId());
            transferVo.setFromName(fromName);
            transferVo.setToName(toName);
            transferVos.add(transferVo);
        }
        return transferVos;
    }

    /**
     * 删除某个账户的转账记录
     * @param userId
     * @param fromId
     * @return
     */
    @Override
    public int delTransferByAcc(String userId, int fromId) {
        QueryWrapper<Transfer> transferQueryWrapper = new QueryWrapper<>();
        transferQueryWrapper
                .eq("user_id", userId)
                .eq("from_id", fromId);
        int res = transferMapper.delete(transferQueryWrapper);
        return res;
    }
}
